//
//  ST_AttractedCell.swift
//  btvideo
//
//  Created by mac on 2020/4/14.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit
import HandyJSON



class ST_AttractedCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.backgroundColor = .white
        contentView.cornerRadius = 20
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview().offset(20)
        }
        
        contentView.addSubview(peculiarityBtn)
        peculiarityBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(titleLabel)
            make.left.equalTo(titleLabel.snp.right).offset(5)
        }
        
        contentView.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.top.equalTo(titleLabel.snp.bottom).offset(3)
        }
        contentView.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.top.equalTo(content_label.snp.bottom).offset(20)
            make.bottom.equalToSuperview()
        }
//        root_view.backgroundColor = .random
        
        contentView.addSubview(moreData)
        moreData.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize.init(width: 200, height: 40))
            make.bottom.equalToSuperview().offset(-20)
            make.centerX.equalToSuperview()
        }
        
        var lastColumnChild:ST_AttractedChildCell?
        
        for index in 0...8 {
            
            let child = ST_AttractedChildCell()
            
//            child.isHidden = true
            
            root_view.addSubview(child)
            
            let row    = index / 3
            let column = index - (row * 3)
            
            print("第\(column)纵列")
            
            switch column {
            case 1:
                child.snp.makeConstraints { (make) in
                    make.centerX.equalToSuperview()
                    make.size.equalTo(ST_AttractedChildCell.size)
                    if let last = lastColumnChild{
                        make.top.equalTo(last.snp.bottom).offset(15)
                    }else{
                        make.top.equalToSuperview()
                    }
                }
            case 2:
                child.snp.makeConstraints { (make) in
                    make.right.equalToSuperview().offset(-10)
                    make.size.equalTo(ST_AttractedChildCell.size)
                    if let last = lastColumnChild{
                        make.top.equalTo(last.snp.bottom).offset(15)
                    }else{
                        make.top.equalToSuperview()
                    }
                }
                lastColumnChild = child
            default:
                child.snp.makeConstraints { (make) in
                    make.left.equalToSuperview().offset(10)
                    make.size.equalTo(ST_AttractedChildCell.size)
                    if let last = lastColumnChild{
                        make.top.equalTo(last.snp.bottom).offset(15)
                    }else{
                        make.top.equalToSuperview()
                    }
                }
            }
        }
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK:
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel()
        control.textColor = UIColor.init(hex: 0x333333)
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.medium)
        return control
    }()
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel()
        control.text = "丰富你的特质，收获更多喜欢"
        control.textColor = UIColor.init(hex: 0xBEC4CF)
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        return control
    }()
    
   fileprivate lazy var peculiarityBtn:UIButton = {
       
       let control = UIButton.init(type: UIButton.ButtonType.custom)
       control.backgroundColor = Color.clear
        control.setTitleColorForAllStates(UIColor.init(hex: 0xFEC207)!)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.medium)
       control.addTarget(self, action: #selector(push_peculiarity_detail), for: UIControl.Event.touchUpInside)
       return control
   }()
    
    fileprivate lazy var root_view:UIView = {
       let control = UIView()
        return control
    }()
    
    fileprivate lazy var moreData:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = Color.init(hex: 0xFEC207)
         control.setTitleColorForAllStates(UIColor.white)
        control.setTitleForAllStates("查看全部")
        control.cornerRadius = 20
         control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)
        control.addTarget(self, action: #selector(push_more_user_detail), for: UIControl.Event.touchUpInside)
        control.isHidden = true
        return control
    }()
    

    //    MARK: - Bind Model
    var model = ST_AttractedDataModel(){
        didSet{
            
            var ta = "他"
//            自己是男
            if ST_BTUserInfoManager.shared.current_account_info?.userinfo?.sex == ST_SEX.male{
                ta = "她"
            }
            
            let text = ta + "们喜欢你的特质"
            
            titleLabel.text = text
            
            peculiarityBtn.setTitleForAllStates(model.feature_name)
            
            for (idx,item) in root_view.subviews.enumerated() {
                
                guard let itemCell = item as? ST_AttractedChildCell else{
                    continue
                }
                if let user = model.user_list[safe:idx]{
                    itemCell.user = user
                    itemCell.isHidden = false
                }else{
                    itemCell.isHidden = true
                }
            }
            
            moreData.isHidden = model.hiddenMoreDataBtn
            
            
        }
    }
    
}

//MARK: - Event
extension ST_AttractedCell{
    
    @objc fileprivate func push_peculiarity_detail(){
        push_more_user_detail()
    }
    
    @objc fileprivate func push_more_user_detail(){
        
        let vc =  ST_PeculiarityDetailShowPersonController()
         
        let pec = ST_PeculiarityModel()
        
        pec.id = model.feature_id
        
        vc.parent_peculiarity = pec
        
        self.parentViewController?.navigationController?.pushViewController(vc)
        
        
    }
    
}

//MARK: - Child Cell
class ST_AttractedChildCell: UIView {
    
    static let size = CGSize.init(width: (ScreenWidth-100)/3, height: (ScreenWidth-100)/3+10+20)
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
//        self.backgroundColor = .random
        
        self.addSubview(avatar_image_view)
        avatar_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(self.snp.width)
            make.top.equalToSuperview()
            make.centerX.equalToSuperview()
        }
        
        self.addSubview(rightSignLabel)
        rightSignLabel.snp.makeConstraints { (make) in
            make.width.equalTo(34)
            make.height.equalTo(18)
            make.right.top.equalToSuperview()
        }
        
        self.addSubview(textLabel)
        textLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(avatar_image_view.snp.bottom).offset(10)
            make.width.lessThanOrEqualTo(120)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK:
    fileprivate lazy var avatar_image_view:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.imageView?.contentMode = .scaleAspectFill
        control.cornerRadius = ST_AttractedChildCell.size.width/2.0
        control.addTarget(self, action: #selector(push_attracted_user_center), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var rightSignLabel:UILabel = {
        let control = UILabel()
        control.text = "新人"
        control.cornerRadius = 9
        control.textAlignment = .center
        control.textColor = .white
        control.backgroundColor = Color.init(hex: 0xFEC207)
        control.font = UIFont.systemFont(ofSize: 10, weight: UIFont.Weight.medium)
        return control
    }()
    
    
    fileprivate lazy var textLabel:UILabel = {
        let control = UILabel()
        control.textColor = UIColor.init(hex: 0x4C4C4C)
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)
        return control
    }()
//    MARK:
    var user = ST_AttractedUser(){
        didSet{
            
            avatar_image_view.sd_setImage(with: user.avatar.compress_small_image(width: 90*3).url, for: UIControl.State.normal, completed: nil)
            
            rightSignLabel.isHidden = !user.is_new_user
            
            
            textLabel.text = user.age.string + "岁" + " " + user.address
        }
    }
    
    @objc fileprivate func push_attracted_user_center(){
        
        let vc = ST_PairsCenterViewController()
        
        vc.accid = user.accid
        
        let nav = PairsCenterInteractiveNavigationController.init(rootViewController: vc)
        
        nav.overCurrentContext = true
        
        GlobalPresent(toController: nav)
        
    }
    
}



//MARK: - Model
class ST_AttractedDataModel:NSObject, HandyJSON {
    
    
    var user_list: Array<ST_AttractedUser> = []
    
    /**
     没有发特质 使用
     */
    var feature_user: Array<ST_AttractedSupplementUser> = []
    
    /**
     默认展示 没有特质界面
     */
    var displaySupplement = true
    
    var fault_msg = ""
    
    var feature_name = ""
    
    var feature_image = ""
    
    var feature_id = 0
    
    var like_num = 0
    /**999个女生喜欢*/
    var supplementStr = ""
    
    required override init() {
        super.init()
    }
    
//    extension Properties
    var hiddenMoreDataBtn = true
    
    var cellHeight = 100.cgFloat
    
    func cell_frame_logic() {
    
        
//        displaySupplement = feature_user.count > 0
        
        
        if displaySupplement{
            
            var gender = "女"
//            自己是女
            if ST_BTUserInfoManager.shared.current_account_info?.userinfo?.sex == ST_SEX.female{
                gender = "男"
            }
            
            supplementStr = "\(like_num)个\(gender)生喜欢"
            
            cellHeight = ST_AttractedSupplementCell.image_item_view_size.height*2 + 5 + 10 + 70
            
            return
        }
        
        
        hiddenMoreDataBtn = user_list.count < 9
        
        let row = user_list.count.double / 3
        
        let rowInt = row.ceil.cgFloat
        
        if !hiddenMoreDataBtn{
            cellHeight = 81 + ST_AttractedChildCell.size.height * rowInt + (rowInt-1)*15  + 80
        }else{
            cellHeight = 81 + ST_AttractedChildCell.size.height * rowInt + (rowInt-1)*15  + 20
        }
    }
    
}

class ST_AttractedSupplementUser:NSObject, HandyJSON {
    
    var content:String = ""
    var width:Float = 1
    var height:Float = 1
    var accid:String = ""
    required override init() {
        super.init()
    }
}



class ST_AttractedUser:NSObject, HandyJSON {
    
    var nickname = ""
    var birth = 0
    var vd_address = ""
    var address = ""
    var create_time = ""
    var avatar = ""
    var age = 0
    var is_new_user =  false
    var accid = ""
    required override init() {
        super.init()
    }
}
